﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CM_Table.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System
Imports Microsoft.Win32

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Customization

Imports FSK_XL

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '

'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '


Public Class CD_ImpLay

    '// --------------------------------------------------------------------- '
    Private Sub GetSheetList(ByRef sFile As String)

        Dim ExcelWorkbook As XLW = Nothing

        Try
            Me.CB_ShLst.Items.Clear()
            If (System.IO.File.Exists(sFile) = False) Then
                CMain.AlertUser(TX.ERR_FILE)
                Exit Try
            end if   
            '// open the worksheet
            ExcelWorkbook = New XLW(sFile)
            If (ExcelWorkbook.ErrSts <> XLW.ERR.OK) Then
                MsgBox(CMain._T(TX.ERR_FILE),MsgBoxStyle.OkOnly,CMain._T(TX.ERR))
                Exit Try
            End If
            
            '// get the sheet list
            Dim sArySht() As String = Nothing
            Dim iActive As Integer
            if ExcelWorkbook.GetSheetList(sArySht, iActive) = False Then
                CMain.AlertUser(tx.ERRSHLST)
                Exit Try
            End If

            For i = 0 to UBound(sArySht)
                Me.CB_ShLst.Items.Add(sArySht(i))
            Next
            Me.CB_ShLst.Text = sArySht(iActive)

		Catch ex As System.Exception
			CMain.DebugException(ex)
            CMain.PopUpExc()
        Finally
            if ExcelWorkbook isnot Nothing Then
                ExcelWorkbook.Close()
                ExcelWorkbook = Nothing
            End If
		End Try

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Function GetOptions(ByRef tLy As TBLLY) As Boolean

        Dim bOpt As Boolean = False

        Dim ExcelSheet As XLS = Nothing
        Dim ExcelWorkbook As XLW = Nothing

        Try
            tLy.bImpTx = Me.CK_ImpTx.Checked

            '// open the worksheet
            ExcelWorkbook = New XLW(Me.tx_DB.Text)
            If (ExcelWorkbook.ErrSts <> XLW.ERR.OK) Then
                MsgBox(CMain._T(TX.ERR_FILE),MsgBoxStyle.OkOnly,CMain._T(TX.ERR))
                Exit Try
            End If
            
            '// attempt to open it
            ExcelSheet = ExcelWorkbook.GetSheet(Me.CB_ShLst.Text)
            If (ExcelSheet Is Nothing) Then
                CMain.AlertUser(tx.ERRWRKS)'// todo
                Exit Try
            End If


            '// get location and size
            If (CMain.GetInt(Me.TX_RS.Text,tLy.lyData.iRowStart) = False) Then
                CMain.AlertUser(tx.NAN)'// todo
                Me.TX_RS.SelectAll()
                Me.TX_RS.Focus()
                Exit Try
            End If
            If (CMain.GetInt(Me.TX_RC.Text,tLy.lyData.iRowCnt) = False) Then
                CMain.AlertUser(tx.NAN)'// todo
                Me.TX_RC.SelectAll()
                Me.TX_RC.Focus()
                Exit Try
            End If
            If (CMain.GetInt(Me.TX_CS.Text,tLy.lyData.iColStart) = False) Then
                CMain.AlertUser(tx.NAN)'// todo
                Me.TX_RS.SelectAll()
                Me.TX_RS.Focus()
                Exit Try
            End If
            If (CMain.GetInt(Me.TX_CC.Text,tLy.lyData.iColCnr) = False) Then
                CMain.AlertUser(tx.NAN)'// todo
                Me.TX_CC.SelectAll()
                Me.TX_CC.Focus()
                Exit Try
            End If

            '// get the layout
            if (ExcelSheet.GetLayOut(tLy.lyData) = False)
                CMain.AlertUser(tx.ERRLAY)
                Exit Try
            End If

            '// get the content, if required
            If (tLy.bImpTx) Then
                ExcelSheet.GetRangeContent( _
                    tLy.lyData.iRowStart, _
                    tLy.lyData.iColStart, _
                    tLy.lyData.iRowCnt, _
                    tLy.lyData.iColCnr, _
                    tLy.sTxCnt)
            End If

            Dim dTmp As Double
            Dim dTmp2 As Double = 0
            If (Me.R_Scale.Checked) Then
                If (CMain.GetTxDbl(Me.TX_Scale, dTmp) = False) Then
                    Exit Try
                End If
                tLy.dScale = dTmp
            ElseIf (Me.R_FitH.Checked) Then
                If (CMain.GetTxDbl(Me.TX_FitH, dTmp) = False) Then
                    Exit Try
                End If
                For i As Integer = 0 to UBound(tLy.lyData.dWidths)
                    dTmp2 = dTmp2 + tLy.lyData.dWidths(i)
                Next
                tLy.dScale = dTmp / dTmp2
            Else'If (Me.R_FitV.Checked) Then
                If (CMain.GetTxDbl(Me.TX_FitV, dTmp) = False) Then
                    Exit Try
                End If
                For i As Integer = 0 to UBound(tLy.lyData.dHeights)
                    dTmp2 = dTmp2 + tLy.lyData.dHeights(i)
                Next
                tLy.dScale = dTmp / dTmp2
            End If


            bOpt = True
		Catch ex As System.Exception
			CMain.DebugException(ex)
            CMain.PopUpExc()
        Finally
            if ExcelWorkbook isnot Nothing Then
                ExcelWorkbook.Close()
                ExcelWorkbook = Nothing
            End If
		End Try


        Return bOpt

    End Function
    '// ===================================================================== '






    '// --------------------------------------------------------------------- '
    Private Sub B_Browse_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Browse.Click

        '// get a file to open
        Dim fdlg As Windows.Forms.OpenFileDialog = _
            New Windows.Forms.OpenFileDialog()
        
        fdlg.Title = CMain._T(tx.SELFILE)
        fdlg.FilterIndex = 1
        fdlg.Filter = CMain._T(tx.EXCFILES) & " (*.xls)|*.xls|" & _
                      CMain._T(tx.ALLFILES) & " (*.*)|*.*"
        fdlg.RestoreDirectory = False
        fdlg.FileName = Me.tx_DB.Text
    LoopOpen:
        If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.tx_DB.Text = fdlg.FileName
            GetSheetList(Me.tx_DB.Text)
        Else
            Return
        End If

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Refresh_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Refresh.Click

        GetSheetList(Me.tx_DB.Text)

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Pick_Loc_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _


    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Pick_Scale_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Pick_Scale.Click

        Me.Hide()
        Dim bOk As Boolean
        Dim dRes As Double
        dRes = CMain.GetScaleFromUser(bOk)

        If bOk = True Then
            Me.TX_Scale.Text = dRes.ToString()
        End If

        Autodesk.AutoCAD.ApplicationServices.Application. _
            ShowModelessDialog(Me)

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Pick_FitH_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Pick_FitH.Click

        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices. _
            Application.DocumentManager.MdiActiveDocument
        If acDoc Is Nothing Then
            Return
        End If

        Me.Hide()

        Dim pdo As New PromptDistanceOptions(CMain._T(TX.TOTTBLW))
        Dim pdr As PromptDoubleResult
        pdo.AllowNegative = False
        pdo.AllowNone = False
        pdo.AllowZero = False
        pdr = acDoc.Editor.GetDistance(pdo)

        If pdr.Status = PromptStatus.OK Then
            Me.TX_FitH.Text = pdr.Value.ToString("0.000")
        End if

        Autodesk.AutoCAD.ApplicationServices.Application. _
            ShowModelessDialog(Me)

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Pick_FitV_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Pick_FitV.Click


        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices. _
            Application.DocumentManager.MdiActiveDocument
        If acDoc Is Nothing Then
            Return
        End If

        Me.Hide()

        Dim pdo As New PromptDistanceOptions(CMain._T(TX.TOTTBLH))
        Dim pdr As PromptDoubleResult
        pdo.AllowNegative = False
        pdo.AllowNone = False
        pdo.AllowZero = False
        pdr = acDoc.Editor.GetDistance(pdo)

        If pdr.Status = PromptStatus.OK Then
            Me.TX_FitV.Text = pdr.Value.ToString("0.000")
        End if

        Autodesk.AutoCAD.ApplicationServices.Application. _
            ShowModelessDialog(Me)

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Pick_TblLoc_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Pick_TblLoc.Click

        Dim ppo As PromptPointOptions
        Dim ppr As PromptPointResult
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices. _
            Application.DocumentManager.MdiActiveDocument
        If acDoc Is Nothing Then
            Return
        End If
        Dim tLy As TBLLY = Nothing

        if GetOptions(tLy) = False Then
            Return
        End If


        tLy.sTxSty = "Standard"


        Me.Hide()

        ppo = New PromptPointOptions(vbLf & CMain._T(TX.PICKPOINT) & ":")
        ppo.UseBasePoint = False
        ppr = acDoc.Editor.GetPoint(ppo)
        If ppr.Status = PromptStatus.OK Then
            tLy.dEast = ppr.Value.X
            tLy.dNorth = ppr.Value.Y

            Using acLock As DocumentLock = acDoc.LockDocument()
                CM_Table.CreateTblFromLay(acDoc,tLy)
            End Using

        End If

        Autodesk.AutoCAD.ApplicationServices.Application. _
            ShowModelessDialog(Me)

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Close_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Close.Click

        Me.Close()

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CD_ImpLay_FormClosing( _
        ByVal sender As System.Object,  _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) _
        Handles MyBase.FormClosing

        If Not (string.IsNullOrEmpty(Me.tx_DB.Text)) Then
            CM_Table.Stgs.sExclImp = Me.tx_DB.Text
        End If
        CM_Table.Stgs.bImpTx = Me.CK_ImpTx.Checked

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CD_ImpLay_Load( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load


        Me.Text = CMain._T(TX.IMPTLAY)

        Me.L_Src.Text = CMain._T(TX.CH_TABLAY)
        Me.B_Browse.Text = CMain._T(TX.BROWSE)
        Me.B_Refresh.Text = CMain._T(TX.REFRESH)
        Me.L_Sheet.Text = CMain._T(TX.CH_SRCSH)
        Me.L_SrcLoc.Text = CMain._T(TX.CH_SRCLOCSZ)
        Me.L_Rows.Text = CMain._T(TX.ROWS)
        Me.L_Cols.Text = CMain._T(TX.COLS)
        Me.L_Start.Text = CMain._T(TX.START)
        Me.L_Cnt.Text = CMain._T(TX.COUNT)

        Me.B_Pick_FitH.Text = CMain._T(TX.PICK)
        Me.B_Pick_FitV.Text = CMain._T(TX.PICK)
        Me.B_Pick_Scale.Text = CMain._T(TX.PICK)
        Me.B_Pick_TblLoc.Text = CMain._T(TX.PICK)

        Me.L_FinalSz.Text = CMain._T(TX.FINSZ)
        Me.R_Scale.Text = CMain._T(TX.SCALE)
        Me.R_FitH.Text = CMain._T(TX.FIT_H)
        Me.R_FitV.Text = CMain._T(TX.FIT_V)

        Me.L_AdOpt.Text = CMain._T(TX.ADOPTS)
        Me.L_SelLoc.Text = CMain._T(TX.SELLOC)
        Me.B_Close.Text = CMain._T(TX.CLOSE)

        Me.CK_ImpTx.Text = CMain._T(TX.IMPTXTOO)


        Me.CK_ImpTx.Checked = CM_Table.Stgs.bImpTx
        Me.tx_DB.Text = CM_Table.Stgs.sExclImp

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub SelectRange
        Dim ExcelSheet As XLS = Nothing
        Dim ExcelWorkbook As XLW = Nothing

        Try
            '// open the worksheet
            ExcelWorkbook = New XLW(Me.tx_DB.Text)
            If (ExcelWorkbook.ErrSts <> XLW.ERR.OK) Then
                MsgBox(CMain._T(TX.ERR_FILE),MsgBoxStyle.OkOnly,CMain._T(TX.ERR))
                Exit Try
            End If
            
            Dim r As Integer, c As Integer, rC As Integer, cC As Integer
            Dim sS As String = Nothing
            If ExcelWorkbook.GetSelByUser(r,c,rC,cC,sS) = True Then
                Me.TX_RS.Text = r.ToString
                Me.TX_RC.Text = rC.ToString
                Me.TX_CS.Text = c.ToString
                Me.TX_CC.Text = cC.ToString
                Me.CB_ShLst.Text = sS
            End If

		Catch ex As System.Exception
			CMain.DebugException(ex)
            CMain.PopUpExc()
        Finally
            if ExcelWorkbook isnot Nothing Then
                ExcelWorkbook.Close()
                ExcelWorkbook = Nothing
            End If
		End Try
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_SelRange_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_SelRange.Click

        SelectRange()
    End Sub
    '// ===================================================================== '
End Class





'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
